#ifndef __HD44780_TYPE_H__
#define __HD44780_TYPE_H__

/* defining a struction for 8-bits data , every bit can be changed singlely */
typedef struct {
	unsigned char b0:1;
	unsigned char b1:1;
	unsigned char b2:1;
	unsigned char b3:1;
	unsigned char b4:1;
	unsigned char b5:1;
	unsigned char b6:1;
	unsigned char b7:1;
} OCTER_BITS ;

typedef union {
	unsigned char byte;
	OCTER_BITS bits;
} OCTER ;
#define SET_OCTER_BY_BITS( OCT , VALUE ) 			\
	do{							\
		unsigned int i , tmp , bit;			\
		tmp = 0x##VALUE;				\
		for( i=0; i<8; i++){				\
			bit = tmp % 16;				\
			tmp /= 16;				\
			switch(i){				\
			case 0:					\
				OCT.bits.b0 = bit;		\
				break; 				\
			case 1:					\
				OCT.bits.b1 = bit;		\
				break; 				\
			case 2:					\
				OCT.bits.b2 = bit;		\
				break; 				\
			case 3:					\
				OCT.bits.b3 = bit;		\
				break; 				\
			case 4:					\
				OCT.bits.b4 = bit;		\
				break; 				\
			case 5:					\
				OCT.bits.b5 = bit;		\
				break; 				\
			case 6:					\
				OCT.bits.b6 = bit;		\
				break; 				\
			case 7:					\
				OCT.bits.b7 = bit;		\
				break; 				\
			}					\
		}						\
	}while(0)

/* define the sturction of address of character generator (CG) */
typedef struct {
	unsigned char CGA_b0:1 ;
	unsigned char CGA_b1:1 ; 
	unsigned char CGA_b2:1 ; 
	unsigned char CGA_b3:1 ; 
	unsigned char CGA_b4:1 ; 
	unsigned char CGA_b5:1 ; 
} CG_BITS ;

typedef union {
	OCTER CGA;
	CG_BITS CGA_BITS;
} CG_ADDR ;
#define SET_CG_ADDR_BY_BITS( CG , VALUE ) 			\
	do{							\
		unsigned int i , tmp , bit;			\
		tmp = 0x##VALUE;				\
		for( i=0; i<6 ; i++){				\
			bit = tmp % 16;				\
			tmp /= 16;				\
			switch(i){				\
			case 0:					\
				CG.CGA_BITS.CGA_b0 = bit;	\
				break; 				\
			case 1:					\
				CG.CGA_BITS.CGA_b1 = bit;	\
				break; 				\
			case 2:					\
				CG.CGA_BITS.CGA_b2 = bit;	\
				break; 				\
			case 3:					\
				CG.CGA_BITS.CGA_b3 = bit;	\
				break; 				\
			case 4:					\
				CG.CGA_BITS.CGA_b4 = bit;	\
				break; 				\
			case 5:					\
				CG.CGA_BITS.CGA_b5 = bit;	\
				break; 				\
			}					\
		}						\
	}while(0)

/* define the sturction of address of display data(DD) */
typedef struct {
	unsigned char DDA_b0:1 ;
	unsigned char DDA_b1:1 ; 
	unsigned char DDA_b2:1 ; 
	unsigned char DDA_b3:1 ; 
	unsigned char DDA_b4:1 ; 
	unsigned char DDA_b5:1 ; 
	unsigned char DDA_b6:1 ; 
} DD_BITS ; 

typedef union {
	OCTER DDA;
	DD_BITS DDA_BITS;
} DD_ADDR ;
#define SET_DD_ADDR_BY_BITS( DD , VALUE ) 			\
	do{							\
		unsigned int i , tmp , bit;			\
		tmp = 0x##VALUE;				\
		for( i=0; i<7 ; i++){				\
			bit = tmp % 16;				\
			tmp /= 16;				\
			switch(i){				\
			case 0:					\
				DD.DDA_BITS.DDA_b0 = bit;	\
				break; 				\
			case 1:					\
				DD.DDA_BITS.DDA_b1 = bit;	\
				break; 				\
			case 2:					\
				DD.DDA_BITS.DDA_b2 = bit;	\
				break; 				\
			case 3:					\
				DD.DDA_BITS.DDA_b3 = bit;	\
				break; 				\
			case 4:					\
				DD.DDA_BITS.DDA_b4 = bit;	\
				break; 				\
			case 5:					\
				DD.DDA_BITS.DDA_b5 = bit;	\
				break; 				\
			case 6:					\
				DD.DDA_BITS.DDA_b6 = bit;	\
				break; 				\
		}						\
	}while(0)

#endif
